package crypto;

import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
import utils.security.encodeUtil;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;


public class ECCUtil {
    static {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }

    //生成秘钥对
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
        keyPairGenerator.initialize(256, new SecureRandom());
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }

    //获取公钥(Base64编码)
    public static String getPublicKey(KeyPair keyPair){
        ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
        byte[] bytes = publicKey.getEncoded();
        return encodeUtil.byte2Base64(bytes);
    }

    //获取私钥(Base64编码)
    public static String getPrivateKey(KeyPair keyPair){
        ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        return encodeUtil.byte2Base64(bytes);
    }

    //将Base64编码后的公钥转换成PublicKey对象
    public static ECPublicKey string2PublicKey(String pubStr) throws Exception{
        byte[] keyBytes = encodeUtil.base642Byte(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
        ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec);
        return publicKey;
    }

    //将Base64编码后的私钥转换成PrivateKey对象
    public static ECPrivateKey string2PrivateKey(String priStr) throws Exception{
        byte[] keyBytes = encodeUtil.base642Byte(priStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
        ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec);
        return privateKey;
    }

    //公钥加密
    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
        Cipher cipher = Cipher.getInstance("ECIES", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    //私钥解密
    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
        Cipher cipher = Cipher.getInstance("ECIES", "BC");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }

    public static void main(String[] args) throws Exception {
//        KeyPair keyPair = ECCUtil.getKeyPair();
//        String publicKeyStr = ECCUtil.getPublicKey(keyPair);
//        String privateKeyStr = ECCUtil.getPrivateKey(keyPair);

        String publicKeyStr = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEHlUpydWlcW1kMLGGXXDgAhS+ZsFH/TPA8/ubGXNsG+7p/W3qVipiHwn2nlvUokBbsmio2mKXXY3WKRsnqqU0Ew==";
        System.out.println("ECC公钥Base64编码:" + publicKeyStr);
        //MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEHlUpydWlcW1kMLGGXXDgAhS+ZsFH/TPA8/ubGXNsG+7p/W3qVipiHwn2nlvUokBbsmio2mKXXY3WKRsnqqU0Ew==
        String privateKeyStr = "MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgfYKl7fpCfBToL9zopt/fdL3JbpZsvWHQKzZohxhryCCgCgYIKoZIzj0DAQehRANCAAQeVSnJ1aVxbWQwsYZdcOACFL5mwUf9M8Dz+5sZc2wb7un9bepWKmIfCfaeW9SiQFuyaKjaYpddjdYpGyeqpTQT";
        System.out.println("ECC私钥Base64编码:" + privateKeyStr);
        //MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgfYKl7fpCfBToL9zopt/fdL3JbpZsvWHQKzZohxhryCCgCgYIKoZIzj0DAQehRANCAAQeVSnJ1aVxbWQwsYZdcOACFL5mwUf9M8Dz+5sZc2wb7un9bepWKmIfCfaeW9SiQFuyaKjaYpddjdYpGyeqpTQT
        ECPublicKey publicKey = string2PublicKey(publicKeyStr);
        ECPrivateKey privateKey = string2PrivateKey(privateKeyStr);

        byte[] publicEncrypt = publicEncrypt("hello world".getBytes(), publicKey);
        System.out.println(encodeUtil.byte2Base64(publicEncrypt));
        byte[] privateDecrypt = privateDecrypt(publicEncrypt, privateKey);
        System.out.println(new String(privateDecrypt));
    }
}